﻿package  {
	import flash.display.MovieClip;
	import flash.events.*;


	public class displaycell extends moveableobj{
		var acell:cell;
		var arrowmode:Boolean=false;
		var usearrow:arrow;
		var tentts:Array=new Array();
		public var wsys:wasys;
		var tentc:Number=3;
		public function displaycell() {
			// constructor code
			acell=new cell(1000);
			//acell.attackrate=30;
			//acell.hpheal=0.1;
			//trace("set",acell.attackrate);
			//acell.attackspeed=0.1;
			//acell.attack=2;
			this.addEventListener(Event.ENTER_FRAME,frelisten);
			this.addEventListener(MouseEvent.MOUSE_DOWN,msdown);
			while(tentts.length<tentc)
			tentts.push(new tentacle());
		}
		public function sysinit(ww:wasys)
		{
			wsys=ww;
			var i:int;
			for(i=0;i<tentts.length;i++)
			{
			wsys.wasence.addChild(tentts[i]);
			}
		}
		public function killtentacle(ia:uint)
		{
			if(tentts[ia].getinitialed())
			{
			tentts[ia].gettarget().removeattack(this);
			
			tentts[ia].uninit();
			}
			
		}
		public function arrowupevent()
		{
			if(arrowmode)
			{
				var tempint:Number;
				var i:uint;
				var j:uint;
				var ttu:int=-1;
				
					arrowmode=false;
					wsys.wasence.removeChild(usearrow);
					usearrow=null;
					var tmparr:Array=wsys.hittest(stage.mouseX,stage.mouseY);
					for(i=0;i<tmparr.length;i++)
					{
						if(tmparr[i]!=this && isattacksomecell(tmparr[i])==false)
						{
							ttu=-1;
							for(j=0;j<tentts.length;j++)
							{
								if(!(tentts[j].getinitialed()))
								{
									ttu=j;
									break;
								}
							}
							if(ttu!=-1)
							{
							tentts[ttu].init(tmparr[i],this);
							tentts[ttu].setgrowtask(1);
							if(tmparr[i].beingattack(this))
							{
								tentts[0].setgrowtask(0.5);
							}
							}
						}
					}
			}
		}
		public function beingattack(f:displaycell):Boolean
		{
			var i:uint;
			var tt:Boolean=false;
			for(i=0;i<tentts.length;i++)
			{
				if(tentts[i].gettarget()==f)
				{
					if(getteam()==f.getteam())
					{
						killtentacle(i);
						//tentts[i].uninit();
					}
					else
					{
					tentts[i].setgrowtask(0.5);
					tt=true;
					}
				}
			}
			return tt;
		}
		public function removeattack(f:displaycell)
		{
			var i:uint;
			
			for(i=0;i<tentts.length;i++)
			{
				if(tentts[i].gettarget()==f)
				{
					tentts[i].setgrowtask(1);
				}
			}
		}
		public function frelisten(aaa:Event)
		{
			acell.glow();
			var temp:int;
			temp=acell.hp;
			label.text=((temp)).toString();
			//trace(acell.hp);
			//wsys.cell2.moveto(stage.mouseX-this.width/2,stage.mouseY-this.height/2);//**test tentacle*/
				//tentts[0].init(wsys.cell2,this);
			for(var j:int=0;j<tentts.length;j++)
			{
			if(tentts[j].getinitialed())
			{
				if(acell.attackcount==0 && acell.getpower())
				{
					//trace("add");
					tentts[j].addtransfer(acell.attackspeed[acell.getstage()],acell.attack);
				}
				tentts[j].growconstantspeed(10);
				tentts[j].transfer();
			}
			}
		}
		public function isattacksomecell(c:displaycell):Boolean
		{
			for(var j:int=0;j<tentts.length;j++)
			{
			if(tentts[j].getinitialed())
			{
				if(tentts[j].gettarget()==c)
				{
					//trace("add");
					tentts[j].addtransfer(acell.attackspeed[acell.getstage()],acell.attack);
					return true;
				}
				
			}
			}
			return false;;
		}
		public function msdown(aaa:MouseEvent)
		{
			if(!arrowmode && wsys.controlteam==getteam() )
			{
			arrowmode=true;
			usearrow=new arrow();
			usearrow.init(this.getcenter());
			wsys.wasence.addChild(usearrow);
			wsys.settallingcell(this);
			}
		}
		public function setteam(a:uint)
		{
			
			//trace(a);
			gotoAndStop(a+1);
			acell.team=a;
		}
		public function getteam()
		{
			return acell.team;
		}
		public function betransfer(force:Number,teams:Number)
		{
			if(acell.betransfer(force,teams))
			{
				setteam(teams);
				killtentacle(0);
			}
		}
		public function cuttentacle(st:vector2,ed:vector2)
		{
			if(wsys.controlteam!=getteam())return;
			var i:uint;
			for(i=0;i<tentts.length;i++)
			{
				if(tentts[i].getinitialed())
				{
				if(tentts[i].cutt(st,ed))
				{
					killtentacle(i);
				}
				}
			}
		}
		
	}
	
}
